package com.huanke.iot.base.commpent;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.util.UUID;

@Component
public class OssUploadTool {

    @Value("${ossAccessKey}")
    private String ossAccessKey;

    @Value("${ossAccessSecret}")
    private String ossAccessSecret;

    public String addFile(String bucketName,byte[] datas){

        final String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";

        String fileName = UUID.randomUUID().toString().replace("-","");

        OSS ossClient = new OSSClient(endpoint, ossAccessKey, ossAccessSecret);

        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,  fileName,new ByteArrayInputStream(datas));

        ossClient.putObject(putObjectRequest);

        ossClient.shutdown();

        return fileName;
    }
}
